package com.xii.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xii.eduservice.entity.EduSubject;
import com.xii.eduservice.entity.excel.SubjectData;
import com.xii.eduservice.entity.subject.OneSubject;
import com.xii.eduservice.entity.subject.TwoSubject;
import com.xii.eduservice.listener.SubjectExcelListener;
import com.xii.eduservice.mapper.EduSubjectMapper;
import com.xii.eduservice.service.EduSubjectService;
import com.xii.servicebase.exceptionhandler.XiiException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author xhjava
 * @since 2022-02-10
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    /**
     * @description: 添加课程分类
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e) {
            e.printStackTrace();
            throw new XiiException(20002,"添加课程分类失败");
        }
    }

    /**
     * @description: 得到树形列表
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //获取一级分类数据记录 parent_id = 0  存入list集合
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.orderByAsc("sort", "id");
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapper);
        //获取二级分类数据记录 parent_id != 0
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("parent_id", 0);
        queryWrapper2.orderByAsc("sort", "id");
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapper2);


        //创建最终要的到的数据列表
        ArrayList<OneSubject> subjectNestedVoArrayList = new ArrayList<>();
        //遍历一级标题集合  封装一级分类  将一级标题集合内容放到最终需要的集合中  使用
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //所有一级标题
            EduSubject eduSubject = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());     这样写比较麻烦 使用工具类BeanUtils 复制另一个对象
            BeanUtils.copyProperties(eduSubject,oneSubject);
            subjectNestedVoArrayList.add(oneSubject);//加入到最终所需集合

            //封装二级分类 遍历所有二级分类
            //创建存储二级分类的集合
            ArrayList<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                EduSubject teduSubject = twoSubjectList.get(j);
                //如果二级标题的父id等于一级标题的id
                if(eduSubject.getId().equals(teduSubject.getParentId())){
                    //创建二级类别vo对象
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(teduSubject,twoSubject);    //前面对象的内容复制到后面对象中
                    //添加到存储二级分类的集合数据列表
                    twoFinalSubjectList.add(twoSubject);
                }

            }
            //将该二级分类列表添加到当前一级列表的Children
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return subjectNestedVoArrayList;
    }
}
